package Player;

import Enemy.SpaceBear;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.image.ImageObserver;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class Player  {

    private int health;
    private int xPos;
    private int yPos;
    private int projXPos;
    private int projYPos;
    private int score;

    private boolean left = false;
    private boolean right = false;
    private boolean up = false;
    private boolean down = false;
    private boolean shoot = false;
    private boolean newShot = false;

    Image playerShip;
    Image playerDead;
    Image projectile;
    Image afterBurner;

    //Projectile ptile;

    public Player(){
        xPos = 200;
        yPos = 600;
        health = 10;
        score = 0;
        projXPos = -100;
        projYPos = 0;
        projectile = new ImageIcon("images/AnimProj1.gif") {}.getImage();
        playerShip = new ImageIcon("images/memeShip.png") {}.getImage();
        playerDead = new ImageIcon("images/playerDead.gif") {}.getImage();
        afterBurner = new ImageIcon("images/afterBurner2.gif") {}.getImage();
    }

    public void setXPos(int xPos){
        this.xPos = this.xPos + xPos;
    }

    public int getScore(){
        return score;
    }

    public void addScore(int score){
        this.score += score;
    }

    public void setScore(int score){
        this.score = score;
    }

    public int getXPos(){
        return xPos;
    }

    public void setYPos(int yPos){
        this.yPos = this.yPos + yPos;
    }

    public int getYPos(){
        return yPos;
    }

    public void resetPositions() {
        xPos = 200;
        yPos = 200;
    }

    public void setHealth(int health){
        this.health = health;
    }

    public void changeHealth(int health){
        this.health = this.health + health;
    }

    public int getHealth(){
        return health;
    }

    public int getProjectileX(){
        return projXPos;
    }

    public void setProjectileX(int x){
        projXPos = x;
    }

    public int getProjectileY(){
        return projYPos;
    }

    public void setProjectileY(int y){
        projYPos = y;
    }

    public void setNewShot(boolean shot){
        newShot = shot;
    }

    public boolean checkIfDead(){
        if(health != 0){
            return false;
        }else{
            return true;
        }
    }

    public void drawPlayer(Graphics g){
        if(checkShot()){
            g.drawImage(projectile, projXPos, projYPos, 19, 50, null);
        }
        if(checkIfDead()){
            g.drawImage(playerDead, xPos, yPos, 100, 100, null);
        }else{
            g.drawImage(afterBurner, xPos+40, yPos+90, 20, 27, null);
            g.drawImage(playerShip, xPos, yPos, 100, 100, null);
        }
    }

    public void checkButtons(int keyCode){
        if(keyCode == KeyEvent.VK_LEFT || keyCode == KeyEvent.VK_KP_LEFT){
            right = false;
            left = true;
        }
        if(keyCode == KeyEvent.VK_RIGHT || keyCode == KeyEvent.VK_KP_RIGHT){
            left = false;
            right = true;
        }
        if(keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_KP_UP){
            down = false;
            up = true;
        }
        if(keyCode == KeyEvent.VK_DOWN || keyCode == KeyEvent.VK_KP_DOWN){
            up = false;
            down = true;
        }
        moveShip();

        if(keyCode == KeyEvent.VK_SPACE && !newShot){
            shoot = true;
        }
    }

    public void moveShip(){
        if(left){
            if(getXPos() > 0){
                setXPos(-2);
            }
        }
        if(right){
            if(getXPos() < 680){
                setXPos(2);
            }
        }
        if(up){
            if(getYPos() > 0){
                setYPos(-2);
            }
        }
        if(down){
            if(getYPos() < 560){
                setYPos(2);
            }
        }
    }

    public void stopShip(int keyCode){
        if(keyCode == KeyEvent.VK_LEFT || keyCode == KeyEvent.VK_KP_LEFT){
            left = false;
        }
        if(keyCode == KeyEvent.VK_RIGHT || keyCode == KeyEvent.VK_KP_RIGHT){
            right = false;
        }
        if(keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_KP_UP){
            up = false;
        }
        if(keyCode == KeyEvent.VK_DOWN || keyCode == KeyEvent.VK_KP_DOWN){
            down = false;
        }

    }
    
    public void startShooting(){
        //System.out.println("startShooting");
        if(shoot && !newShot && health > 0){
            projXPos = getXPos()+40;
            projYPos = getYPos()+40;
            newShot = true;
        }else if(newShot){
            shooting();
        }
    }

    public void shooting(){
        //System.out.println("shooting");
        if(newShot){
            projYPos = projYPos - 5;
            //System.out.println(projYPos);
        }
        if(checkProjectile()){
            setProjectileX(-1000);
            setProjectileY(-1000);
            newShot = false;
        }
        if(shoot && !newShot){
            startShooting();
        }
    }

    public void stopShooting(int keyCode){
        //System.out.println("stopShooting");
        if(keyCode == KeyEvent.VK_SPACE){
            shoot = false;
        }
    }

    public boolean checkShot(){
        if(newShot == true){
            return true;
        }
        return false;
    }

    public boolean checkProjectile(){
        if(projYPos <= 0){
           setProjectileX(-1000);
           setProjectileY(-1000);
           return true;
        }
        return false;
    }

}